var gulp=require('gulp');
var $=require('gulp-load-plugins')();
var open=require('open');

var app={
    srcPath:'src/',
    devPath:'build/',
    prdPath:'dist/'
};

//库文件
/*gulp.task('lib',function(){
    gulp.src('node_modules/angular/*.js')
        .pipe(gulp.dest(app.prdPath+'vendor'))
        .pipe(gulp.dest(app.devPath+'vendor'))
        .pipe($.connect.reload());
});*/
gulp.task('lib',function(){
    gulp.src('bower_components/**/*')
        .pipe(gulp.dest(app.prdPath+'vendor'))
        .pipe(gulp.dest(app.devPath+'vendor'))
        .pipe($.connect.reload());
});

//拷贝html文件
gulp.task('html',function(){
    gulp.src(app.srcPath+'**/*.html')
        .pipe(gulp.dest(app.prdPath))
        .pipe(gulp.dest(app.devPath))
        .pipe($.connect.reload());
});

//编译less
gulp.task('style',function(){
    gulp.src(app.srcPath+'style/*.css')
        .pipe(gulp.dest(app.devPath+'css'))
        .pipe($.cssmin())
        .pipe(gulp.dest(app.prdPath+'css'))
        .pipe($.connect.reload());
});

//拷贝js文件
gulp.task('js',function(){
    gulp.src(app.srcPath+'script/*.js')
        .pipe($.concat('index.js'))
        .pipe(gulp.dest(app.devPath+'js'))
        .pipe($.uglify())
        .pipe(gulp.dest(app.prdPath+'js'))
        .pipe($.connect.reload());
});

//拷贝image
gulp.task('image',function(){
    gulp.src(app.srcPath+'image/*')
        .pipe(gulp.dest(app.devPath+'image'))
        .pipe($.imagemin())
        .pipe(gulp.dest(app.prdPath+'image'))
        .pipe($.connect.reload());
});

//清除
gulp.task('clean',function(){
    gulp.src([app.prdPath,app.devPath])
        .pipe($.clean());
});

//打包执行
gulp.task('build',['lib','html','style','js','image']);

//服务
gulp.task('serve',['build'],function(){
    $.connect.server({
        root:[app.devPath],
        port: 8080,
        livereload: true
    });
    gulp.watch(app.srcPath+'script/*.js',['js']);
    gulp.watch(app.srcPath+'**/*.html',['html']);
    gulp.watch(app.srcPath+'style/*.css',['style']);
    gulp.watch(app.srcPath+'image/*',['image']);

});

//在命令行中只使用gulp完成运行serve
gulp.task('default',['serve']);


//测试
//gulp.tsak('s',function(){
//    $.connect.server({
//        root: 'build',
//        livereload:true
//    });
//    gulp.watch('bulid/**/*.*',['reload']);
//});

//gulp.task('reload',function(){
//   gulp.src(app.devPath+'*.html')
//   .pipe($.connect.reload());
//});